﻿using System;
using System.Text;
using System.Web;
using System.Net;
using System.Net.Mail;
using CDONTS;

namespace LimpaGaragem.Framework
{
    public class Mail
    {
        /// <summary>
        /// Send mail with "System.Net.Mail"
        /// </summary>
        /// <param name="SMTPServer"></param>
        /// <param name="host"></param>
        /// <param name="senderMail"></param>
        /// <param name="recipientMail"></param>
        /// <param name="subject"></param>
        /// <param name="body"></param>
        /// <param name="htmlFormat"></param>
        /// <param name="highPriority"></param>
        /// <returns></returns>
        public bool SendMail(string SMTPServer, int host, string senderMail, string recipientMail, string subject, string body, bool htmlFormat, bool highPriority)
        {
            bool ok = true;
            MailMessage objMailMessage = new MailMessage();
            SmtpClient objSmtpClient = new SmtpClient();

            try
            {
                if (SMTPServer.Trim() != "" && host > 0)
                {
                    objSmtpClient.Host = SMTPServer.Trim();
                    objSmtpClient.Port = host;

                    if (senderMail.Trim() != "" && recipientMail.Trim() != "")
                    {
                        System.Net.Mail.MailAddress _mailAdress = new MailAddress(senderMail);
                        objMailMessage.Subject = subject;
                        objMailMessage.Body = body;
                        objMailMessage.From = _mailAdress;
                        objMailMessage.To.Add(recipientMail);
                        if (highPriority) { objMailMessage.Priority = MailPriority.High; }
                        else { objMailMessage.Priority = MailPriority.Normal; }
                        objMailMessage.IsBodyHtml = htmlFormat;
                        objMailMessage.BodyEncoding = Encoding.UTF8;
                        objSmtpClient.Credentials = CredentialCache.DefaultNetworkCredentials;
                        objSmtpClient.DeliveryMethod = SmtpDeliveryMethod.PickupDirectoryFromIis;
                        objSmtpClient.Send(objMailMessage);
                    }
                    else
                    {
                        ok = false;
                    }
                }
                else
                {
                    ok = false;
                }
            }
            catch (Exception ex)
            {
                ok = false;
                throw ex;
            }

            return ok;
        }
        
        /// <summary>
        /// Send mail with "CDONTS"
        /// </summary>
        /// <param name="senderMail"></param>
        /// <param name="recipientMail"></param>
        /// <param name="recipientMailCopy"></param>
        /// <param name="recipientMailCopyHidden"></param>
        /// <param name="subject"></param>
        /// <param name="body"></param>
        /// <param name="format">0-HTML / 1-Text</param>
        /// <param name="priority">0-Low / 1-Normal/ 2-High</param>
        /// <returns></returns>
        public bool SendMail(string senderMail, string recipientMail, string recipientMailCopy,
                               string recipientMailCopyHidden, string subject, string body, byte format, byte priority)
        {
            bool ok = true;
            NewMail objNewMail = new NewMail();

            if (senderMail.Trim() != "" && recipientMail.Trim() != "")
            {
                try
                {
                    if (recipientMailCopy.Trim() != "") { objNewMail.Cc = recipientMailCopy.Trim(); }
                    if (recipientMailCopyHidden.Trim() != "") { objNewMail.Bcc = recipientMailCopyHidden.Trim(); }

                    objNewMail.From = senderMail;
                    objNewMail.To = recipientMail;
                    objNewMail.Subject = subject;
                    objNewMail.Body = body;
                    objNewMail.BodyFormat = format;
                    objNewMail.MailFormat = 0;
                    objNewMail.Importance = priority;
                    objNewMail.Send(senderMail, recipientMail, subject, body, priority);
                }
                catch (Exception ex)
                {
                    ok = false;
                    throw ex;
                }
            }
            else
            {
                ok = false;
            }
 
            return ok;
        }
    }
}
